package com.skeryl.sqlite.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.skeryl.sqlite.model.Chapter;

public class ChapterDAO
{
	// private DBOpenHelper helper;

	// public ChapterDAO(Context context)
	// {
	// helper = new DBOpenHelper(context);
	// }

	public ChapterDAO()
	{
		getDAO();
	}

	public void getDAO()
	{
		db = new DBO().getDatabase();
	}

	private SQLiteDatabase db;

	/**
	 * 添加新的信息
	 * 
	 * @param chapter
	 */
	public void add(Chapter chapter)
	{
		// db = helper.getWritableDatabase();

		db.execSQL("insert into t_chapter (sid,title,content) values (?,?,?)", new Object[]
		{ chapter.getSid(), chapter.getTitle(), chapter.getContent() });

	}

	/**
	 * 更新信息
	 * 
	 * @param chapter
	 */
	public void update(Chapter chapter)
	{
		// db = helper.getWritableDatabase();

		db.execSQL("update t_chapter set title = ?,content = ? where sid = ?", new Object[]
		{ chapter.getTitle(), chapter.getContent(), chapter.getSid() });
	}

	/**
	 * 查找信息
	 * 
	 * @param sid
	 * @return
	 */
	public Chapter find(int sid)
	{
		// db = helper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select sid,title,content from t_chapter where sid = ?", new String[]
		{ String.valueOf(sid) });
		if (cursor.moveToNext())
		{
			return new Chapter(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("title")), cursor.getString(cursor.getColumnIndex("content")));
		}
		return null;
	}
	/**
	 * 查找信息
	 * 
	 * @param sid
	 * @return
	 */
	public Chapter find(String sid)
	{
		// db = helper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select sid,title,content from t_chapter where sid = ?", new String[]
		{ sid });
		if (cursor.moveToNext())
		{
			return new Chapter(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("title")), cursor.getString(cursor.getColumnIndex("content")));
		}
		return null;
	}
	/**
	 * 获取信息
	 * 
	 * @param start
	 *            起始位置
	 * @param count
	 *            数量
	 * @return
	 */
	public List<Chapter> getScrollData(int start, int count)
	{

		// db = helper.getWritableDatabase();

		List<Chapter> chapters = new ArrayList<Chapter>();
		Cursor cursor = db.rawQuery("select sid,title,content from t_chapter limit ?,?", new String[]
		{ String.valueOf(start), String.valueOf(count) });
		while (cursor.moveToNext())
		{
			chapters.add(new Chapter(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("title")), cursor.getString(cursor.getColumnIndex("content"))));
		}
		return chapters;
	}

	/**
	 * 获取数量
	 * 
	 * @return
	 */
	public long getCount()
	{
		// db = helper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select count(sid) from t_chapter", null);
		if (cursor.moveToNext())
		{
			return cursor.getInt(0);
		}

		return 0;
	}

	public void close()
	{
		// helper.close();
		db.close();
	}

	public void closeAddGet()
	{
		// helper.close();
		try
		{
			db.close();
		} catch (Exception e)
		{

		}

		getDAO();
	}
}